﻿using LyData.Data;
using System;
using System.Data;
using System.Web;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_ManagerRole : System.Web.UI.Page
{
    protected void AddClass_Click(object sender, EventArgs e)
    {
        if (base.IsValid)
        {
            IDb db = new DbFactory().DriverDb();
            db.Open();
            string[,] includeColumns = new string[,] { { "Title", this.RoleTitle.Text, "NVarChar", "32" }, { "SiteLanguage", SiteInfo.SiteLanguage(), "int", "0" } };
            db.Insert("managerRole", includeColumns);
            db.Close();
            this.Msg.Text = "会员组添加成功！";
            this.Msg.Visible = true;
            this.RoleTitle.Text = "";
            this.Bind();
        }
    }

    protected void Bind()
    {
        IDb db = new DbFactory().DriverDb();
        db.Open();
        DataTable table = db.DataTable("RoleId,Title,AddTime,UpTime", "managerRole", "", "AddTime ASC");
        db.Close();
        this.ManagerRoleList.DataSource = table;
        this.ManagerRoleList.DataBind();
    }

    protected void CustomValidator1_ServerValidate1(object source, ServerValidateEventArgs args)
    {
        IDb db = new DbFactory().DriverDb();
        db.Open();
        int num = db.Scalar("count([Title])", "managerRole", string.Format("Title='{0}' ", args.Value.ToString()));
        db.Close();
        if (num > 0)
        {
            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }
    }

    protected void ManagerRoleList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.ManagerRoleList.PageIndex = e.NewPageIndex;
        this.Bind();
    }

    protected void ManagerRoleList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        this.ManagerRoleList.EditIndex = -1;
        this.Bind();
    }

    protected void ManagerRoleList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        HyperLink link = (HyperLink)e.Row.FindControl("EditHyperLink");
        Button button = (Button)e.Row.FindControl("DelBt");
        if (button != null)
        {
            if (!ManagerLogin.ValidateRole("C_ManagerRoleEdit.aspx"))
            {
                link.Enabled = false;
            }
            if (!ManagerLogin.ValidateRole("C_ManagerRole.aspx_b"))
            {
                button.Enabled = false;
            }
        }
    }

    protected void ManagerRoleList_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int rowIndex = e.RowIndex;
        string str = this.ManagerRoleList.DataKeys[rowIndex].Values[0].ToString();
        IDb db = new DbFactory().DriverDb();
        db.Open();
        if (db.Scalar("count(roleId)", "manager", string.Format("roleId LIKE '%,{0},%'", str)) > 0)
        {
            this.Msg.CssClass = "errorMsg";
            this.Msg.Text = "组删除出错：还有管理员属于此组";
            this.Msg.Visible = true;
        }
        else
        {
            db.Delete("managerRole", string.Format("RoleId={0}", str));
            db.Close();
            this.Bind();
            this.Msg.Text = "组删除成功！";
            this.Msg.Visible = true;
        }
    }

    protected void ManagerRoleList_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.ManagerRoleList.EditIndex = e.NewEditIndex;
        this.Bind();
    }

    protected void ManagerRoleList_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        bool isValid = base.IsValid;
        this.ManagerRoleList.EditIndex = -1;
        this.Bind();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!ManagerLogin.ValidateRole("C_ManagerRole.aspx_a"))
        {
            this.AddClass.Enabled = false;
        }
        this.Msg.CssClass = "msg";
        this.Msg.Visible = false;
        if (!base.IsPostBack)
        {
            this.Bind();
        }
    }
}
